//
//  ClityListDatePickViewController.m
//  基本框架
//
//  Created by runone02 on 2018/8/23.
//  Copyright © 2018年 liqiyun. All rights reserved.
//

#import "ClityListDatePickViewController.h"
#import "ClityListDatePick.h"
#import "AddressPickView.h"  //三级选择
#import "AddressChoicePickerView.h"//四级选择

@interface ClityListDatePickViewController ()
/** 按钮*/
@property (nonatomic, strong) UIButton *btn;
/** 三级选择*/
@property (nonatomic,strong) UIButton *dizhiBtn;
/** 四级选择*/
@property (nonatomic,strong) UIButton *sijiBtn;
@end

@implementation ClityListDatePickViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupNavView];
    [self loadBtn];
}

- (void)setupNavView{
    [self.navView.leftButton setTitle:@"返回" forState:UIControlStateNormal];
    [self.navView.centerButton setTitle:@"选择城市" forState:UIControlStateNormal];
}

- (void)loadBtn{
    self.btn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.btn.layer.borderWidth = 1.0f;
    self.btn.layer.borderColor = [UIColor lightGrayColor].CGColor;
    [self.btn setTitle:@"选择城市" forState:UIControlStateNormal];
    [self.btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.btn.titleLabel.font = TITLE_FONT(30);
    self.btn.layer.cornerRadius = 50;
    self.btn.clipsToBounds = YES;

    [self.view addSubview:self.btn];
    [self.btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(self.view).with.offset(50 + kTopBarHeight);
        make.width.height.mas_equalTo(100);
    }];
    
    self.dizhiBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.dizhiBtn.layer.borderWidth = 1.0f;
    self.dizhiBtn.layer.borderColor = [UIColor lightGrayColor].CGColor;
    [self.dizhiBtn setTitle:@"选择城市" forState:UIControlStateNormal];
    [self.dizhiBtn addTarget:self action:@selector(showAddressPickView) forControlEvents:UIControlEventTouchUpInside];
    [self.dizhiBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.dizhiBtn.titleLabel.font = TITLE_FONT(30);
    self.dizhiBtn.layer.cornerRadius = 50;
    self.dizhiBtn.clipsToBounds = YES;
    
    [self.view addSubview:self.dizhiBtn];
    [self.dizhiBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.btn.mas_bottom).with.offset(20);
        make.centerX.mas_equalTo(self.view);
        make.width.height.mas_equalTo(100);
    }];
    
    self.sijiBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.sijiBtn.layer.borderWidth = 1.0f;
    self.sijiBtn.layer.borderColor = [UIColor lightGrayColor].CGColor;
    [self.sijiBtn setTitle:@"选择城市" forState:UIControlStateNormal];
    [self.sijiBtn addTarget:self action:@selector(tapPress:) forControlEvents:UIControlEventTouchUpInside];
    [self.sijiBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.sijiBtn.titleLabel.font = TITLE_FONT(30);
    self.sijiBtn.layer.cornerRadius = 50;
    self.sijiBtn.clipsToBounds = YES;
    
    [self.view addSubview:self.sijiBtn];
    [self.sijiBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.dizhiBtn.mas_bottom).with.offset(20);
        make.centerX.mas_equalTo(self.view);
        make.width.height.mas_equalTo(100);
    }];
    
}

- (void)btnClick:(UIButton *)btn{
    WeakSelf(weakSelf);
    NSString * dic = [[NSBundle mainBundle] pathForResource:@"city" ofType:@"plist"];
    
    NSArray *data = [[NSArray alloc] initWithContentsOfFile:dic];
    ClityListDatePick * clity = [[ClityListDatePick alloc]initWithFrame:self.view.bounds];
    clity.dataArr = data;
    
    clity.block = ^(NSString *cityList1,NSString *cityList2){        
        NSString *str = [NSString stringWithFormat:@"%@%@",cityList1,cityList2];
        [weakSelf.btn setTitle:str forState:UIControlStateNormal];
    };
    [self.view addSubview:clity];
}

- (void)showAddressPickView{
    WeakSelf(weakSelf);
    AddressPickView *addressPickView = [AddressPickView shareInstance];
    [self.view addSubview:addressPickView];
    addressPickView.block = ^(NSString *province,NSString *city,NSString *town){
        NSString *str = [NSString stringWithFormat:@"%@ %@ %@",province,city,town] ;
        [weakSelf.dizhiBtn setTitle:str forState:UIControlStateNormal];
    };
}

- (void)tapPress:(UIButton *)sender {
    WeakSelf(weakSelf);
    AddressChoicePickerView *addressPickerView = [[AddressChoicePickerView alloc]init];
    addressPickerView.block = ^(AddressChoicePickerView *view,UIButton *btn,AreaObject *locate){
        NSString *str = [NSString stringWithFormat:@"%@",locate];
        [weakSelf.sijiBtn setTitle:str forState:UIControlStateNormal];
    };
    [addressPickerView show];
}
@end
